//加载公共样式
require('../../assets/css/index.less')
const request= require('../../utils/request')

//字体图标
require('../../assets/fonts/iconfont.css')
//加载自己的样式
require('./login.less')


window.onload = () => {
    let phoneIpt = document.querySelector('#phoneIpt')
    let pwdIpt = document.querySelector('#pwdIpt')
    let submit = document.querySelector('#submit')
    submit.addEventListener('click', function () {
        let errMsg = ''
        if (!/^1[3456789]\d{9}$/.test(phoneIpt.value)) {
            errMsg = '手机号由11位数字组成'
        } else if (!/^\w{6,12}$/.test(pwdIpt.value)) {
            errMsg = '密码由6-12位字母或数字组成'
        } else {
            request.post('/api/user/login',{
                account: phoneIpt.value,
                password: pwdIpt.value
            }).then(res=>{
                console.log(res);
                let { errno, data } = res.data
                if(errno==0){
                    localStorage.setItem('token',data.token)
                    location.href='./home.html'
                    alert('登陆成功')
                }else{
                    alert('登陆失败')
                }
            })
        }

        if (errMsg) {
            alert(errMsg)
        }
    })
}